home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 32 / sot.zip / SOTG.C < prev    next >
Text File  |  1988-08-23  |  6KB  |  249 lines

  1. /********** The Son of Tetris Project ************/
  2.  
  3. /************  GAME ***********/
  4.  
  5. #include <conio.h>
  6. #include <dos.h>
  7. #include <setjmp.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. #include "sot.h"
  12.  
  13. #define MAX_LEVEL 9
  14. #define ACT_LEVEL 3  /* level at which extra shapes start appearing */
  15. #define MIN_SHAPES 7 /* minimum number of shapes */
  16. #define ADD_SHAPE MIN_SHAPES - ACT_LEVEL + 1
  17. #define MAX_SHAPE 20 /* maximum number of shapes allowed before upping level */
  18.  
  19. #define DELAY_TIME 120 /* Loop, unit of time */
  20.  
  21. int      mv(drctn_type dt, OBJ_TYPE *cur_obj, int amt);   /* move object */
  22. int      insrt(OBJ_TYPE *cur_obj,SHP_TYPE *next_sh);      /* insert object */
  23.  
  24.  
  25.  
  26. static  SHP_TYPE *next_sh;  /* next shape to appear */
  27. static  unsigned  level;    /* Level at which game is being played */
  28. static  long      score;
  29. static  unsigned  lines_del;/* Lines deleted */
  30. static  int       sound_on = TRUE; /* Controls use of sound FX */
  31. static  int       show_shape = TRUE; /* show next shape */
  32. static  unsigned  shape_count;
  33.  
  34. static void get_next_shape(void)
  35. {
  36.   unsigned new_shape_no, choice_of_shapes;
  37.   if (level < ACT_LEVEL)
  38.     choice_of_shapes = MIN_SHAPES;
  39.   else
  40.     choice_of_shapes = (no_of_shapes < level + ADD_SHAPE) ?
  41.                        no_of_shapes : level + ADD_SHAPE;
  42.   new_shape_no = (rand() % choice_of_shapes);
  43.   for (next_sh = shp_lst; new_shape_no; new_shape_no--)
  44.     next_sh = next_sh -> next_shp;
  45. }    /* get_next_shape */
  46.  
  47.  
  48.  
  49. static void do_blip(void)
  50. {
  51.   if (sound_on)
  52.     sound(100);
  53.   delay(40); nosound();
  54. } /* do_blip */
  55.  
  56. static void end_noise(void)
  57. {
  58.   int i;
  59.   if (!sound_on)
  60.     return;
  61.   for (i = 1000; i < 3000; i+= 10)
  62.   {
  63.     sound(i); delay(1);
  64.   }
  65.   nosound();
  66. } /* end_noise */
  67.  
  68.  
  69. static void   kill_row(int t_row)
  70. {
  71.   int x,y;
  72.   for (y = t_row; y > 1; y--)
  73.     for (x = 1; x < BLW - 1; x++)
  74.       arena[x][y] = arena[x][y-1];
  75.   for (x = 1; x < BLW - 1; x++)
  76.       arena[x][1] = CLEAR;
  77.   v_kill_row(t_row);
  78.   p_arena(1,1,BLW-1,t_row);
  79. }      /* kill_row */
  80.  
  81.  
  82. static int   check_full_row(void)
  83. {
  84.   int y,x;
  85.   int clear_found, result;
  86.  
  87.   result = FALSE;
  88.   for (y = BLH - 2; y > 1; y--)
  89.   {
  90.     clear_found = FALSE;
  91.     for (x = 1; x <= BLW -2; x++)
  92.       if (arena[x][y] == CLEAR)
  93.       {
  94.     clear_found = TRUE;
  95.     break;
  96.       }
  97.     if (clear_found)
  98.       continue;
  99.     lines_del++;  result = TRUE;
  100.     kill_row(y);
  101.     y++;
  102.   }
  103.   return(result);
  104. } /* check_full_row */
  105.  
  106.  
  107. static void  handle_keys(OBJ_TYPE *cur_obj, int *fn_key)
  108. {
  109.   extern jmp_buf    end_game;
  110.   int  i;           /* Index to Number of keys allowed per loop */
  111.  
  112.   static unsigned delay_period[MAX_LEVEL + 1] =
  113.         {120, 98, 80, 66, 54, 44, 36, 29, 24,/* 20*/ 15};
  114.  
  115.   for (i = 0; i < NO_OF_KEY_PRESSES; i++)
  116.   {
  117.     if (kbhit())   /* get any keys */
  118.       switch(getch())
  119.       {
  120.     case   0:                    /* Beginning of two code key */
  121.       *fn_key = TRUE;    /* Warn next iteration */
  122.       i--;              /* Overide loop */
  123.       continue;         /* Try again */
  124.  
  125.         case HOME_KEY:              /* Go Left */
  126.       if (!*fn_key)
  127.         break;
  128.  
  129.         case '7':
  130.           mv(left, cur_obj, HORIZ_MOVE);
  131.       break;
  132.  
  133.  
  134.         case UP_KEY:                /* Rotate */
  135.       if (!*fn_key)
  136.         break;
  137.  
  138.         case '8':
  139.           mv(anti, cur_obj, HORIZ_MOVE);
  140.       break;
  141.  
  142.  
  143.         case PGUP_KEY:             /* Go right */
  144.       if (!*fn_key)
  145.         break;
  146.  
  147.         case '9':
  148.           mv(right, cur_obj, HORIZ_MOVE);
  149.       break;
  150.  
  151.         case LEFT_KEY:             /* Drop */
  152.       if (!*fn_key)
  153.         break;
  154.  
  155.         case '4':
  156.     case ' ':
  157.           while (!mv(down,cur_obj, VERT_MOVE));
  158.       break;
  159.  
  160.  
  161.         case RIGHT_KEY:             /* Level up */
  162.       if (!*fn_key)
  163.         break;
  164.  
  165.         case '6':
  166.       if (level < MAX_LEVEL)
  167.       {
  168.             level++;
  169.         shape_count  = 0;
  170.             update_score(level, score, lines_del, next_sh, show_shape);
  171.       }
  172.       break;
  173.  
  174.         case 'p':                /* Pause game */
  175.     case 'P':
  176.       if (!pause_game())
  177.         break;
  178.  
  179.         case ESC:                /* Quit game */
  180.       longjmp(end_game,1);
  181.       break;
  182.  
  183.  
  184.         case 's':
  185.     case 'S':
  186.       sound_on = !sound_on;
  187.       break;
  188.  
  189.     default:
  190.       /* Do nothing */
  191.       break;
  192.       } /* switch */
  193.  
  194.     *fn_key = FALSE; /* Cancel function key */
  195.     delay(delay_period[level]);  /* delay after action */
  196.   } /* for */
  197. }  /* handle_keys */
  198.  
  199.  
  200.  
  201. void   game()
  202. {
  203.   OBJ_TYPE *cur_obj;
  204.   int  fn_key;      /* Boolean flag used to trap two code keys */
  205.   unsigned local_score;
  206.  
  207.   do
  208.   {
  209.     init_arena();
  210.     p_arena(0,0,(BLW-1),(BLH-1));   /*  display blank arena */
  211.     level = get_level();
  212.     score = 0L; lines_del = 0;
  213.     cur_obj = malloc(sizeof(OBJ_TYPE));
  214.     fn_key = FALSE; shape_count = 0;
  215.     get_next_shape(); /* get first shape */
  216.     while (!insrt(cur_obj,next_sh))
  217.     {
  218.       get_next_shape();
  219.       update_score(level, score, lines_del, next_sh, show_shape);
  220.       handle_keys(cur_obj,&fn_key);  /* at top */
  221.  
  222.       for (local_score = 24 + 3*level;
  223.          !mv(down,cur_obj, VERT_MOVE);
  224.          local_score--)
  225.         handle_keys(cur_obj,&fn_key);
  226.  
  227.       if (check_full_row())
  228.         do_blip();
  229.       score += local_score;
  230.       if (((lines_del/10) > level) && (level < MAX_LEVEL))
  231.       {
  232.         level++;
  233.     shape_count = 0;
  234.       }
  235.       if (!(++shape_count % MAX_SHAPE) && (level < MAX_LEVEL))
  236.         level++;
  237.     } /* while can insert */
  238.     update_score(level, score, lines_del, next_sh, FALSE);
  239.     end_noise();
  240.     while (kbhit())   /* Empty keyboard buffer */
  241.       getch();
  242.   }
  243.   while (get_another_go(score));   /* Dialogue with player between games */
  244.  
  245. } /* game */
  246.  
  247.  
  248.  
  249.